package com.glory.wxelectricitypda.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.FTChangeMantissaAdapter;
import com.glory.wxelectricitypda.adapter.SameWorkorderlotsAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.FTPittedSubmitBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FTChangeMantissaActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.rv_recycler)
    RecyclerView rv_recycler;
    private String equipmentid;
    private String subequipmentidlist;
    private String lotid;
    private ArrayList<Parcelable> ftneedmergelotlist;
    private FTChangeMantissaAdapter mFTChangeMantissaAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_ftchange_mantissa;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("FT进站");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                submitNew();
                submitEND();
            }
        });

        rv_recycler.setLayoutManager(new LinearLayoutManager(FTChangeMantissaActivity.this));
        rv_recycler.setItemAnimator(new DefaultItemAnimator());
        rv_recycler.addItemDecoration(new DividerItemDecoration(FTChangeMantissaActivity.this, 1));
        mFTChangeMantissaAdapter = new FTChangeMantissaAdapter(null);
        mFTChangeMantissaAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mFTChangeMantissaAdapter.isFirstOnly(true);
        rv_recycler.setAdapter(mFTChangeMantissaAdapter);
    }

    @Override
    protected void initData() {

        Intent intent = getIntent();
        equipmentid = intent.getStringExtra("EQUIPMENTID");
        subequipmentidlist = intent.getStringExtra("SUBEQUIPMENTIDLIST");
        lotid = intent.getStringExtra("LOTID");
        ftneedmergelotlist = intent.getParcelableArrayListExtra("FTNEEDMERGELOTLIST");

        if (ftneedmergelotlist.size()>0){
            mFTChangeMantissaAdapter.setNewData((List)ftneedmergelotlist);
        }
    }

    private void submitEND(){

        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONArray lotList = new JSONArray();
        JSONObject lotObject = new JSONObject();
        JSONArray lotList01 = new JSONArray();
        try {
            jsonObject1.put("ACTIONTYPE", "FTTrackIn");
            jsonObject1.put("EQUIPMENTID", equipmentid);
            jsonArray.put(subequipmentidlist);
            jsonObject1.put("SUBEQUIPMENTIDLIST", jsonArray);
            jsonObject1.put("ISCHECKSTEPAUTHORITY", true);
            lotObject.put("LOTID",lotid);
            lotList.put(lotObject);
            jsonObject1.put("LOTLIST", lotList);
            ArrayList<Integer> lotList1 = mFTChangeMantissaAdapter.getLotList();
            for (int i = 0;i<lotList1.size();i++){
                JSONObject object = new JSONObject();
                object.put("LOTID",mFTChangeMantissaAdapter.getData().get(lotList1.get(i)).getLOTID());
                lotList01.put(object);
            }
            jsonObject1.put("FTNEEDMERGELOTLIST", lotList01);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostUtils("lotFtTrackin","lotFtTrackin",jsonObject1,this, new FreeUI_DialogEntityCallBack<ResponseBean<FTPittedSubmitBean>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<FTPittedSubmitBean>> response) {

                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())){
                    showToast("进站成功", true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent();
                            setResult(Activity.RESULT_OK,intent);
                            finish();
                        }
                    }, 2000);
                }

            }

            @Override
            public void onError(Response<ResponseBean<FTPittedSubmitBean>> response) {
                super.onError(response);
                loadError(response.getException(), "lotFtTrackin");
            }
        });


    }


    private void submit(boolean RELLYSUBMIT){

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONArray lotList = new JSONArray();
        JSONObject lotObject = new JSONObject();
        JSONArray lotList01 = new JSONArray();
        try {
            jsonObject1.put("ACTIONTYPE", "FTTrackIn");
            jsonObject1.put("EQUIPMENTID", equipmentid);
            jsonArray.put(subequipmentidlist);
            jsonObject1.put("SUBEQUIPMENTIDLIST", jsonArray);
            jsonObject1.put("ISCHECKSTEPAUTHORITY", true);
            jsonObject1.put("RELLYSUBMIT", RELLYSUBMIT);
            lotObject.put("LOTID",lotid);
            lotList.put(lotObject);
            jsonObject1.put("LOTLIST", lotList);
            ArrayList<Integer> lotList1 = mFTChangeMantissaAdapter.getLotList();
            for (int i = 0;i<lotList1.size();i++){
                JSONObject object = new JSONObject();
                object.put("LOTID",mFTChangeMantissaAdapter.getData().get(lotList1.get(i)).getLOTID());
                lotList01.put(object);
            }
            jsonObject1.put("FTNEEDMERGELOTLIST", lotList01);
            jsonObject.put("Data", jsonObject1);
            jsonObject.put("MessageName", "EUIT.LOTFTTRACKIN");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                new TypeToken<ResponseBean<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<Object>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    showToast("提交成功", true);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent();
                            setResult(Activity.RESULT_OK,intent);
                            finish();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<ResponseBean<Object>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTFTTRACKIN");
            }
        });



    }


    private void submitNew(){

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        JSONArray lotList = new JSONArray();
        JSONObject lotObject = new JSONObject();
        JSONArray lotList01 = new JSONArray();
        try {
            jsonObject1.put("ACTIONTYPE", "FTTrackIn");
            jsonObject1.put("EQUIPMENTID", equipmentid);
            jsonArray.put(subequipmentidlist);
            jsonObject1.put("SUBEQUIPMENTIDLIST", jsonArray);
            jsonObject1.put("ISCHECKSTEPAUTHORITY", true);
            jsonObject1.put("RELLYSUBMIT", false);
            lotObject.put("LOTID",lotid);
            lotList.put(lotObject);
            jsonObject1.put("LOTLIST", lotList);
            ArrayList<Integer> lotList1 = mFTChangeMantissaAdapter.getLotList();
            for (int i = 0;i<lotList1.size();i++){
                JSONObject object = new JSONObject();
                object.put("LOTID",mFTChangeMantissaAdapter.getData().get(lotList1.get(i)).getLOTID());
                lotList01.put(object);
            }
            jsonObject1.put("FTNEEDMERGELOTLIST", lotList01);
            jsonObject.put("Data", jsonObject1);
            jsonObject.put("MessageName", "EUIT.LOTFTTRACKIN");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<FTPittedSubmitBean>>(
                new TypeToken<ResponseBean<FTPittedSubmitBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<FTPittedSubmitBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    if (response.body().getBody().getTRACKSHOWMSG().isEmpty()){
                        showToast("提交成功", true);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent();
                                setResult(Activity.RESULT_OK,intent);
                                finish();
                            }
                        }, 2000);

                    }else {
                        FreeApi_DialogUtils.getConfirmDialog(FTChangeMantissaActivity.this, "确定", response.body().getBody().getTRACKSHOWMSG(), new FreeUI_CommonCallback() {
                            @Override
                            public void onSuccess() {
                                submit(response.body().getBody().getRELLYSUBMIT());
                            }
                        }).show(getSupportFragmentManager(),"DIALOG");
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<FTPittedSubmitBean>> response) {
                super.onError(response);
                loadError(response.getException(), "LOTFTTRACKIN");
            }
        });



    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }


}